const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

router.post('/menu/roleMenu', async ctx => {
    let roleId = ctx.request.body.roleId;
    let tid = ctx.user.tenantId;

    let sql = `select case when b.roleId is null then 0 else 1 end checked,a.id,a.name,a.name title,a.pid,a.isBill,a.isReport,a.sort 
             from (
             select a.* from menu_v5 a inner join tenant_menu b on a.id=b.menuId and b.tenantId=${tid} and a.display=1
             ) a left join (
                select * from role_menu where roleId=${roleId}
             ) b on a.id=b.menuId where isnull(a.isDelete,0)=0  order by a.sort`
    let ret = await db.query(sql, {type: QueryTypes.SELECT})
    return ctx.suc('', ret)
})

module.exports = router;
